package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 老师---换题
 * 
 */
@Api(tags = { "V01", "作业系统" })
@CrossOrigin
@RestController
public class WorkAssignQuesChangeController2 extends YxyApiBaseController {

    @Autowired
    private ElasticQuesService elasticQuesService;

    @ApiOperation(value = "作业系统老师换题")
    @RequestMapping(value = "/api_yw/manage/tec/homework/workassign/ques/change2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws JsonParseException, JsonMappingException, IOException {

        // 查询一道试题
        QuesReqModel quesModel1 = reqModel.quesModel1;
        String quesId = reqModel.quesId;
        List<QuesReqModel> quesModelList = reqModel.quesModelList;
        List<String> quesIdList = new ArrayList<String>();
        quesModelList.forEach(quesModel2 -> {
            quesIdList.add(quesModel2.quesId);
        });

        ElasticYxyYxyQuesResource ques = elasticQuesService.findByEsQuesId(quesId);
        String questionType = ques.getQuestionType();
        List<Integer> knowIdList = quesModel1.know;
        String difficultLevel = ques.getDifficultyLevel();
        // 根据该题的id,查询知识点,题型,难度
        ElasticYxyYxyQuesResource ques2 = elasticQuesService.findByQuestionTypeAndQuesIdIn(questionType, knowIdList,
                difficultLevel, quesIdList);
        Assert.notNull(ques2, "没有可换的试题了");

        List<QuesReqModel> list2 = new ArrayList<QuesReqModel>();
        QuesReqModel quesReqModel = null;
        for (int i = 0; i < quesModelList.size(); i++) {
            quesReqModel = quesModelList.get(i);
            String oldid = quesReqModel.quesId;
            if (StringUtils.equals(oldid, quesId)) {
                quesReqModel = new QuesReqModel();
                quesReqModel.quesId = ques2.getId();
                // quesReqModel.knowIdList = knowIdList;
                quesReqModel.know = ques2.getKnowledgeIds();
            }
            list2.add(quesReqModel);
        }
        RespModel respModel = new RespModel();
        respModel.quesIdList = list2;
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 第一次初始化的试题
        public QuesReqModel quesModel1;
        // 要换的题id
        public String quesId;
        // 本次作业的所选的题
        public List<QuesReqModel> quesModelList;

    }

    protected static class QuesReqModel {
        // 题id
        public String quesId;
        // 题涉及的知识点
        public List<Integer> know;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 试题id列表
        public List<QuesReqModel> quesIdList;
    }

}
